package com.apiit.webserviceclient.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class Authentication_DAO {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;

	private String[] allColumns = { MySQLiteHelper.COLUMN_AUTHENTICATION_ID,
			MySQLiteHelper.COLUMN_AUTHENTICATION_NAME,
			MySQLiteHelper.COLUMN_USER_NAME, MySQLiteHelper.COLUMN_PASSWORD,

	};

	public Authentication_DAO(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Authentication createAction(Authentication action) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_AUTHENTICATION_ID,
				action.getAuthentication_id());
		values.put(MySQLiteHelper.COLUMN_AUTHENTICATION_NAME,
				action.getuser_name());
		values.put(MySQLiteHelper.COLUMN_USER_NAME, action.getuser_name());
		values.put(MySQLiteHelper.COLUMN_PASSWORD, action.getpassword());

		long insertId = database.insert(MySQLiteHelper.TABLE_AUTHENTICATION,
				null, values);
		Cursor cursor = database.query(MySQLiteHelper.TABLE_AUTHENTICATION,
				allColumns, MySQLiteHelper.COLUMN_AUTHENTICATION_ID + " = "
						+ insertId, null, null, null, null);
		cursor.moveToFirst();
		Authentication newAction = cursorToAction(cursor);
		cursor.close();
		return newAction;
	}

	public void updateAuthenticationDetails(long authenticationID,
			String authenticationName, String username, String password) {

		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_AUTHENTICATION_NAME,
				authenticationName);
		values.put(MySQLiteHelper.COLUMN_USER_NAME, username);
		values.put(MySQLiteHelper.COLUMN_PASSWORD, password);

		// updating row
		database.update(MySQLiteHelper.TABLE_AUTHENTICATION, values,
				MySQLiteHelper.COLUMN_AUTHENTICATION_ID + " = ?",
				new String[] { String.valueOf(authenticationID) });
	}

	public void deleteAction(Authentication action) {
		long id = action.getAuthentication_id();
		System.out.println("Comment deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_AUTHENTICATION,
				MySQLiteHelper.COLUMN_AUTHENTICATION_ID + " = " + id, null);
	}

	public Authentication getAction(long actionID) {
		Cursor cursor = database.query(MySQLiteHelper.TABLE_AUTHENTICATION,
				allColumns, MySQLiteHelper.COLUMN_AUTHENTICATION_ID + " = "
						+ actionID, null, null, null, null);
		cursor.moveToFirst();
		Authentication newAction;
		if (cursor.getCount() != 0) {
			newAction = cursorToAction(cursor);
		} else {
			newAction = null;
		}
		cursor.close();
		return newAction;
	}

	public List<Authentication> getAllAction() {
		List<Authentication> actions = new ArrayList<Authentication>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_AUTHENTICATION,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Authentication action = cursorToAction(cursor);
			actions.add(action);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return actions;
	}

	private Authentication cursorToAction(Cursor cursor) {

		Authentication action = new Authentication();
		action.setAuthentication_id(cursor.getLong(0));
		action.setauthentication_name(cursor.getString(1));
		action.setuser_name(cursor.getString(2));
		action.setpassword(cursor.getString(3));
		return action;
	}
}